#include<iostream>
#include<map>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
const int maxn=223456;
int n,m;
//struct node{
//int x,y;
//bool friend operator<(node a,node b){
//return a.x<b.x;
//}
//}st[maxn];
long long sum=0;
ll ma[maxn],a[maxn],b[maxn];
int main()
{
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    cin>>n>>m;
    for(int i=1;i<=m;i++){
            ll x,y;
           cin>>x>>y;
    a[x]++;
    b[y]++;

    }
    a[0]=0;
    b[0]=0;
    ma[1]=a[1];
    for(int i=2;i<=n;i++){
        a[i]+=a[i-1];
        b[i]+=b[i-1];
        if(b[i]==b[i-1]){
            ma[i]=a[i]-b[i];
        }
        else{
            ma[i]=a[i]-b[i]+(b[i]-b[i-1]);
        }
        //cout<<"A:"<<a[i]<<"  B:"<<b[i]<<endl;
        //cout<<ma[i]<<endl;
    }
    sort(ma+1,ma+n+1);
    for(int i=1;i<=n;i++){
        sum+=(i*ma[i]);
    }
    cout<<sum<<endl;
    return 0;
}
/*
7 5
2 3
3 7
5 6
3 5
3 3
*/
